package Thread;

public class ThreadDemo12 {
    static class WaitTask implements Runnable{
        Object locker=null;

        public WaitTask(Object locker) {
            this.locker = locker;
        }

        @Override
        public void run() {

            synchronized (locker){
                System.out.println("wait开始");
                //直接调用wait相当于this.wait(),也就是WaitTask调用wait()
                //NotifyTask要求针对同一个对象来通知,但WaitTask对象并不容易获得
                //所以要创建一个构造方法创建一个对象来调用wait()
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("wait结束");
            }

        }
    }
    static class NotifyTask implements Runnable{
        //notify()同理
        Object locker=null;
        public NotifyTask(Object locker) {
            this.locker = locker;
        }
        @Override
        public void run() {
            synchronized (locker){
                System.out.println("唤醒开始");
                //locker.notify();
                locker.notifyAll();
                System.out.println("唤醒结束");

            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Object object=new Object();
        Thread t11=new Thread(new  WaitTask(object));
        Thread t12=new Thread(new  WaitTask(object));
        Thread t13=new Thread(new WaitTask(object));
        Thread t2=new Thread(new NotifyTask(object));
        t11.start();
        t12.start();
        t13.start();
        Thread.sleep(3000);
        t2.start();
    }
}
